﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace HighFantasyRPG
{

    public enum NPCType
    {
        Person = 1,

        Child = 2




    }

    public class NPC
    {
        private Texture2D Rectangular;
        private Animation LeftAnim, RightAnim, DownAnim, UpAnim;
        public int X, Y, Width, Height;
        public AI aiscript;
        public Collision collision;
        private AnimationPlayer sprite;
        private SpriteEffects flip;
        private NPCType npcType;
        private float movement;
        public int direction;
        public string Name;

        public Level Level
        {
            get { return level; }
        }
        Level level;

        // Physics state
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }
        Vector2 position;

        public Vector2 Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }
        Vector2 velocity;

        public Rectangle localBounds;

        public NPC(string name ,Level level, int X, int Y, int Width, int Height, NPCType type)
        {
            this.Name = name;
            this.level = level;
            this.X = X * 32;
            this.Y = Y * 32;
            this.Width = Width;
            this.Height = Height;
            this.npcType = type;
            this.collision = new Collision();
            Position = new Vector2(this.X, this.Y);
            aiscript = new AI(2, 5);
            LoadContent();

        }

        public void LoadContent()
        {



            switch (npcType)
            {

                case NPCType.Person:

                  LeftAnim = new Animation(Level.Content.Load<Texture2D>("Sprites/Person/" + Name + "_left"), 0.5f, true, 3);
                  RightAnim = new Animation(Level.Content.Load<Texture2D>("Sprites/Person/" + Name + "_right"), 0.5f, true, 3);
                  UpAnim = new Animation(Level.Content.Load<Texture2D>("Sprites/Person/" + Name + "_up"), 0.5f, true, 3);
                  DownAnim = new Animation(Level.Content.Load<Texture2D>("Sprites/Person/" + Name + "_down"), 0.5f, true, 3);
                  Rectangular = Level.Content.Load<Texture2D>("Background/Player_rect");
                    break;


                case NPCType.Child:

                  LeftAnim = new Animation(Level.Content.Load<Texture2D>("Sprites/Child/" + Name + "_left"), 0.5f, true, 3);
                  RightAnim = new Animation(Level.Content.Load<Texture2D>("Sprites/Child/" + Name + "_right"), 0.5f, true, 3);
                  UpAnim = new Animation(Level.Content.Load<Texture2D>("Sprites/Child/" + Name + "_up"), 0.5f, true, 3);
                  DownAnim = new Animation(Level.Content.Load<Texture2D>("Sprites/Child/" + Name + "_down"), 0.5f, true, 3);
                    Rectangular = Level.Content.Load<Texture2D>("Background/Critter_rect");
                    break;

            }



            localBounds = new Rectangle(X * 32, Y * 32, Width, Height);


            sprite.PlayAnimation(LeftAnim);
        }

        public void Update(GameTime gameTime, KeyboardState keyboardState)
        {



            aiscript.Update();
            switch (aiscript.actualcommand)
              {
                case 0:
                      
                    if ((!collision.NPCCollisionRight(level.player, this, level.objects, level.enemies)))
             {
                 direction = 1;
                movement = -1.0f;
                X += (int)movement;
                sprite.PlayAnimation(LeftAnim);
             }
                    break;


                case 1:
                    
                if ((!collision.NPCCollisionLeft(level.player, this, level.objects, level.enemies)))
                {
                    direction = 2;
                movement = 1.0f;
                X += (int)movement;
                sprite.PlayAnimation(RightAnim);
                }

                    break;

                


              }

            
            
            position = new Vector2(X, Y);
            Position = position;
            
        }


        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            // Flip the sprite to face the way we are moving.
            if (Velocity.X > 0)
                flip = SpriteEffects.None;


            // Draw that sprite.
            sprite.Draw(gameTime, spriteBatch, Position, flip);
            spriteBatch.Draw(Rectangular, Position, Color.White);
        }

    }
}
